package com.yonyou.pmclouds.visitcard.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.exception.ConcurrentOperationException;
import com.yonyou.pmclouds.basecom.lock.AutoReleaseLockUtil;
import com.yonyou.pmclouds.picture.rmiitf.PictureRemoteService;
import com.yonyou.pmclouds.visitcard.entity.VisitCardVO;
import com.yonyou.pmclouds.visitcard.mapper.VisitCardMapper;
import com.yonyou.pmclouds.visitcard.rmiitf.VisitCardMaintain;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = VisitCardMaintain.class)
public class VisitCardMaintainImpl implements VisitCardMaintain {

    private static final String LOCK_PREFIX = "VisitCard_";

    @Autowired
    private VisitCardMapper visitCardMapper;
    @Autowired
    private PictureRemoteService pictureRemoteService;

    @Override
    public VisitCardVO insert(VisitCardVO vo) throws BusinessException {
        //  add lock
        if (!AutoReleaseLockUtil.addAutoReleaseLock(LOCK_PREFIX + vo.getPkTenant())) {
            throw new ConcurrentOperationException();
        }
        SingleInsertBpTemplate bp = new SingleInsertBpTemplate(visitCardMapper);
        bp.insert(vo);
        // 保存图片
        pictureRemoteService.insertPictureForPub(vo, null);
        return vo;
    }
}
